package com.stevencheong.matrixSolver;

import java.text.NumberFormat;

import javax.vecmath.Tuple3d;
import javax.vecmath.Vector3d;

import Jama.Matrix;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class VectorSolver extends Activity {
	
	private EditText a1;
	private EditText a2;
	private EditText a3;
	
	private EditText b1;
	private EditText b2;
	private EditText b3;
	
	private EditText c1;
	private EditText c2;
	private EditText c3;
	
	private TextView vecAlabel;
	private TextView vecBlabel;
	private TextView vecClabel;
	
	Vector3d a_vector;
	Vector3d b_vector;
	Vector3d c_vector;
	
	private VectorSolver mContext;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        
       	setContentView(R.layout.vector);
        
       	// Initialize EditText's
        a1 = (EditText) findViewById(R.id.a1);
        a2 = (EditText) findViewById(R.id.a2);
        a3 = (EditText) findViewById(R.id.a3);
        
        b1 = (EditText) findViewById(R.id.b1);
        b2 = (EditText) findViewById(R.id.b2);
        b3 = (EditText) findViewById(R.id.b3);
        
        c1 = (EditText) findViewById(R.id.c1);
        c2 = (EditText) findViewById(R.id.c2);
        c3 = (EditText) findViewById(R.id.c3);
        
        vecAlabel = (TextView) findViewById(R.id.vecAlabel);
        vecBlabel = (TextView) findViewById(R.id.vecBlabel);
        vecClabel = (TextView) findViewById(R.id.vecClabel);
        
        // Initialize Buttons
        Button vAddButton = (Button) findViewById(R.id.btnVadd);
        Button vSubtractButton = (Button) findViewById(R.id.btnVsubtract);
        Button vDotProductButton = (Button) findViewById(R.id.btnVdotProduct);
        Button vCrossProductButton = (Button) findViewById(R.id.btnVcrossProduct);
        
        Button vAnorm = (Button) findViewById(R.id.btnAnorm);
        Button vBnorm = (Button) findViewById(R.id.btnBnorm);
        Button vCtoA = (Button) findViewById(R.id.btnCtoA);
        Button vCtoB = (Button) findViewById(R.id.btnCtoB);
        
        // Add Listeners to Vector Buttons
        vAddButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					double[] a_arr = { new Double(a1.getText().toString()), new Double(a2.getText().toString()), new Double(a3.getText().toString()) };
					double[] b_arr = { new Double(b1.getText().toString()), new Double(b2.getText().toString()), new Double(b3.getText().toString()) };
					
					Tuple3d a_vector = new Tuple3d(a_arr) {};
					Tuple3d b_vector = new Tuple3d(b_arr) {};
					
					a_vector.add(b_vector);	//a_vector = a_vector + b_vector
					c_vector = new Vector3d(a_vector);
					
					setVectorCTextFields();
				} catch (NumberFormatException e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vSubtractButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					double[] a_arr = { new Double(a1.getText().toString()), new Double(a2.getText().toString()), new Double(a3.getText().toString()) };
					double[] b_arr = { new Double(b1.getText().toString()), new Double(b2.getText().toString()), new Double(b3.getText().toString()) };
					
					Tuple3d a_vector = new Tuple3d(a_arr) {};
					Tuple3d b_vector = new Tuple3d(b_arr) {};
					
					a_vector.sub(b_vector);	//a_vector = a_vector + b_vector
					c_vector = new Vector3d(a_vector);
					
					setVectorCTextFields();
				} catch (NumberFormatException e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vDotProductButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoVectors();
					
					double result = a_vector.dot(b_vector);
					double angle = a_vector.angle(b_vector);
					
					NumberFormat numberFormatter =  NumberFormat.getNumberInstance();
					numberFormatter.setMinimumFractionDigits(1);
					
					c1.setText( new Double( result ).toString() );
					c2.setText( numberFormatter.format(Math.toDegrees(angle)) + " deg");
					c3.setText( numberFormatter.format(angle) + " rad" );
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vCrossProductButton.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					putInputsIntoVectors();
					
					a_vector.cross(a_vector, b_vector);
					c_vector = new Vector3d(a_vector);
					
					setVectorCTextFields();
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vAnorm.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					Double ax = new Double(a1.getText().toString());
					Double ay = new Double(a2.getText().toString());
					Double az = new Double(a3.getText().toString());
					
					Double length = Math.sqrt((ax * ax) + (ay * ay) + (az * az));
					
					double[] a_arr = { ax/length, ay/length, az/length };
					Tuple3d a_vector = new Tuple3d(a_arr) {};
					c_vector = new Vector3d(a_vector);
					
					setVectorCTextFields();
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vBnorm.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					Double ax = new Double(b1.getText().toString());
					Double ay = new Double(b2.getText().toString());
					Double az = new Double(b3.getText().toString());
					
					Double length = Math.sqrt((ax * ax) + (ay * ay) + (az * az));
					
					double[] a_arr = { ax/length, ay/length, az/length };
					Tuple3d a_vector = new Tuple3d(a_arr) {};
					c_vector = new Vector3d(a_vector);
					
					setVectorCTextFields();
					
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vCtoA.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					a1.setText(c1.getText());
					a2.setText(c2.getText());
					a3.setText(c3.getText());
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
        
        vCtoB.setOnClickListener(new OnClickListener() {
        	private AlertDialog show;
			public void onClick(View arg0) {
				
				try {
					b1.setText(c1.getText());
					b2.setText(c2.getText());
					b3.setText(c3.getText());
				} catch (Exception e) {
					show = new AlertDialog.Builder(mContext).setTitle("Cannot Compute")
							.setMessage("Some inputs are empty")
							.setPositiveButton("OK", null).show(); 
				}
			}
		});
    }
    
    //helper methods
	private void putAInputsIntoVectorA() {
		double[] a_arr = { new Double(a1.getText().toString()), new Double(a2.getText().toString()), new Double(a3.getText().toString()) };
		a_vector = new Vector3d(a_arr);
	}
	
	private void putBInputsIntoVectorB() {
		double[] b_arr = { new Double(b1.getText().toString()), new Double(b2.getText().toString()), new Double(b3.getText().toString()) };
		b_vector = new Vector3d(b_arr);
	}
    
	private void putInputsIntoVectors() {
		putAInputsIntoVectorA();
		putBInputsIntoVectorB();
	}
	
	private void setVectorCTextFields() {
		c1.setText( new Double( c_vector.x ).toString() );
		c2.setText( new Double( c_vector.y ).toString() );
		c3.setText( new Double( c_vector.z ).toString() );
	}
    
}